package cn.itcast.usermanage.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.itcast.usermanage.mapper.UserMapper;
import cn.itcast.usermanage.pojo.User;
import cn.itcast.usermanage.result.DataGridResult;

@Service
public class UserService {
	@Autowired
	private UserMapper userMapper;

	/**
	 * 分页查询方法
	 * @param page
	 * @param rows
	 * @return
	 */
	public DataGridResult<User> queryUserListByPage(Integer page, Integer rows) {
		//查询总条数
		//Long total = this.userMapper.queryTotalCount();
		//查询分页数据
		//List<User> users = this.userMapper.queryUserListByPage((page-1)*rows,rows);
		
		//使用分页助手开始分页
		PageHelper.startPage(page,rows);
		//然后分页拦截器自动对接下来的查询进行分页
		List<User> users = this.userMapper.queryUserList();
		//封装分页信息对象
		PageInfo<User> pageInfo = new PageInfo<>(users);
		//封装页面数据对象
		DataGridResult<User> result = new DataGridResult<>(pageInfo.getTotal(),users);
		return result;
	}

	/**
	 * 新增用户的功能
	 * @param user
	 */
	public void saveUser(User user) {
		this.userMapper.insertUser(user);
	}

	/**
	 * 删除用户功能
	 * @param ids
	 */
	public void deleteUserByIds(Long[] ids) {
		this.userMapper.deleteUserByIds(ids);
	}
	
	
}
